﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNetCore;

namespace GYCTOAManagementSystemNewApi.Public
{
    /// <summary>
    /// 读取配置文件信息，此种方式需要在网站项目中的Startup类中依赖注入IConfiguration对象
    /// </summary>
    public class ConfigurationHelper
    {
        private static IConfiguration _config;
        static IWebHostEnvironment _webHost;
        public static void Configure(IConfiguration config, IWebHostEnvironment webHost)
        {
            _config = config;
            _webHost = webHost;
        }
        /// <summary>
        /// 根据配置文件键读取对应的值
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static string GetConfigValueByKey(string Key)
        {
    
            var value = _config[Key];
            return value;
        }
        public static string GetConfigHeadRoleId(string key)
        {
            string filepath = Path.Combine(_webHost.ContentRootPath, "appsettings.json");
            var text = System.IO.File.ReadAllText(filepath);
            JObject json = JObject.Parse(text);
           var  value= (string)json["System"][key] ;
            return value;
        }

      
        public void WriteJSON(string Key,string value)
        {
            string filepath = Path.Combine(_webHost.ContentRootPath, "appsettings.json");
            var text=System.IO.File.ReadAllText(filepath);
            JObject json = JObject.Parse(text);
            json["System"][Key] = value;
            string result=json.ToString();
            System.IO.File.WriteAllText(filepath, result);
        }
    }
}
